package mapProperties;

import java.io.File;
import java.util.Vector;


public class EntityFactory {
    /**
	 * Path to the entitys files (including trailing "\\")
	 * @uml.property  name="path"
	 */
    String path;

    /**
	 * Entity class name list (stored as strings).  Does not include package prefix.
	 * @uml.property  name="entityNames"
	 * @uml.associationEnd  multiplicity="(0 -1)" elementType="java.lang.String"
	 */
    Vector entityNames;


 /**
  * Default constructor.  The Entity class name list is built from files
  * in .\classes\sbccdraw\Entitys\ .
  */
 public EntityFactory() {
   init(".\\classes\\arcturus\\");
 }

 /**
  * Allows Entitys class name list to be built from files in a specified
  * directory.
  *
  * @param path Path to the Entitys files (including trailing "\\")
  */
 public EntityFactory(String path) {
   init(path);
 }

    /**
     * Loads Entity files and populates the entityNames list.
     * @param path Path to the Entitys files (including trailing "\\")
     * 
     * @uml.property name="path"
     */
    private void init(String path) {
         Class c = null;
         Object o = null;
         String className = "";

         try {
              this.path = path;

              // Build the entityNames list
              entityNames = new Vector();
              File file = new File(path);
              String[] filenames = file.list(); // Get directory listing
              for (int i = 0; i < filenames.length; i++) { // Go through all files
                   if (filenames[i].endsWith(".class") == true) { // Ignore if not a Java .class file
                        if (filenames[i].compareTo("Entity.class") != 0) { // Skip the Entity base class
                             // Try to load the class
                             try {
                                  // Strip ".class" from the end
                                  className = filenames[i].substring(0, filenames[i].length() - 6);

                                  // Load the class and create an instance of it
                                  c = Class.forName("arcturus." + className);
                                  o = c.newInstance();
                             } catch (Exception ex) {
                                  ex.printStackTrace();
                             }

                             // Verify that it is a Entity.  If so, add its class name to the
                             // entity list.
                             if (o instanceof Entity) {
                                  entityNames.addElement(className);
                             }
                        }
                   }
              }
         } catch (Exception ex) {
         }
    }

 /**
* Returns a Vector of entity class names
* @uml.property  name="entityNames"
*/
 public Vector getEntityNames() {
   return (Vector)entityNames.clone();
 }

 /**
  * Create an instance of the specified entity.
  * @param entityName Class name of the entity to create (sans package prefix and
  * .class suffix.
  * @return The new Entity
  * @throws Exception
  */
 public Entity createEntity(String entityName) throws Exception {
   try {
     Class c = Class.forName("arcturus." + entityName);
     Entity d = (Entity)c.newInstance();
     return d;
   }
   catch (Exception ex) {
     throw ex;
   }
 }
}
